/**
 * 
 */
package com.ai.notices.web.controller.busi;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ai.notices.common.util.PagedResult;
import com.ai.notices.dao.mysql.model.AuthStaff;
import com.ai.notices.dao.mysql.model.IdentityCard;
import com.ai.notices.dao.mysql.model.IdentityCardImp;
import com.ai.notices.service.interfaces.IIdentityCardSV;
import com.ai.notices.web.FileUtils;
import com.ai.notices.web.system.AppBaseController;
import com.ai.notices.web.system.util.StaffSessionUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * @author chenjf6 号码管理
 */
@Controller
@RequestMapping(value = "/identity_card")
public class IdentityCardController extends AppBaseController {
	
    private static final Logger log = Logger.getLogger(IdentityCardController.class);

    @Autowired
    private IIdentityCardSV identityCardSV;
	
	@RequestMapping(value = { "/card_init" })
	public String cardInit(Model model) throws Exception {
		
		return "/busi/card/card-init";
	}
	
	@RequestMapping(value = { "/card_list" }, method = { RequestMethod.POST })
	public String cardList(Model model, IdentityCard identityCard, Integer pageNo, Integer pageSize) throws Exception {
		PagedResult<IdentityCard> pageResult = identityCardSV.queryIdentityCardByPage(identityCard, pageNo, pageSize);
        model.addAttribute("data", pageResult);
		return "/busi/card/card-list";
	}
	
	@RequestMapping(value = { "/card_import_init" })
	public String cardImportInit(Model model) throws Exception {
		
		return "/busi/card/card-import";
	}
	
	@RequestMapping(value = { "/card_import" }, method = { RequestMethod.POST })
	@ResponseBody
	public JSONObject cardImport(@RequestParam("cardFile") MultipartFile mfile, Model model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		AuthStaff authStaff = StaffSessionUtil.getStaffInfo(request.getSession());
		JSONObject ret = new JSONObject();
		List<List<Map<String, String>>> cardInfos = FileUtils.file2List(mfile);
		if (CollectionUtils.isEmpty(cardInfos) || CollectionUtils.isEmpty(cardInfos.get(0))) {
			ret.put("flag", false);
			ret.put("msg", "上传的文件格式不正确（如：文件为空、导入文件不支持）");
			return ret;
		}
		String batchId = FileUtils.getId();
		try {
			identityCardSV.saveIdentityCardImp(cardInfos.get(0), batchId, authStaff);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
        ret.put("flag", true);
		ret.put("msg", batchId);
		return ret;
	}
	
	@RequestMapping(value = { "/save_card" }, method = { RequestMethod.POST })
	@ResponseBody
	public JSONObject saveCard(@RequestParam("batchId") String batchId, Model model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		AuthStaff authStaff = StaffSessionUtil.getStaffInfo(request.getSession());
		JSONObject ret = new JSONObject();
		try {
			identityCardSV.saveIdentityCard(batchId, authStaff);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
        ret.put("flag", true);
		ret.put("msg", batchId);
		return ret;
	}
	
	@RequestMapping(value = { "/card_import_list" }, method = { RequestMethod.POST })
	public String cardImportList(Model model, IdentityCardImp imp, Integer pageNo, Integer pageSize) throws Exception {
		PagedResult<IdentityCardImp> pageResult = identityCardSV.queryIdentityCardImpByPage(imp, pageNo, pageSize);
        model.addAttribute("data", pageResult);
		return "/busi/card/card-import-list";
	}
	
	@RequestMapping(value = { "/card_import_success_list" }, method = { RequestMethod.POST })
	public String cardImportSuccessList(Model model, IdentityCardImp imp, Integer pageNo, Integer pageSize) throws Exception {
		PagedResult<IdentityCardImp> pageResult = identityCardSV.queryIdentityCardImpByPage(imp, pageNo, pageSize);
        model.addAttribute("data", pageResult);
		return "/busi/card/card-import-success-list";
	}
	
	@RequestMapping(value = { "/card_import_fail_list" }, method = { RequestMethod.POST })
	public String cardImportFailList(Model model, IdentityCardImp imp, Integer pageNo, Integer pageSize) throws Exception {
		PagedResult<IdentityCardImp> pageResult = identityCardSV.queryIdentityCardImpByPage(imp, pageNo, pageSize);
        model.addAttribute("data", pageResult);
		return "/busi/card/card-import-fail-list";
	}
}
